package yns.controller.exam;

import javax.annotation.Resource;

import org.json.JSONArray;
import org.json.JSONObject;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yns.base.BaseSupport;
import yns.entity.User;
import yns.service.ServiceExam1;

//Controller如果都是rest接口，可以直接使用这个注解，方法不用再添加@ResponseBody
@RestController
public class ControllerExam2 {
	
	//service 注解方式注入
	@Resource(name = "serviceExam1")
	private ServiceExam1 serviceExam1;


	//Rest接口ajax获得的数据直接为json的对象。（无需再parse）
	@RequestMapping(value = "/rest", produces = { BaseSupport.JSON_UTF8 })
	public String rest() {
		System.out.println("rest");
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("info", "中文");
		jsonObject.put("key2", new JSONArray());
		jsonObject.put("html", "");

		return jsonObject.toString();

	}
	
	//接受json格式的数据
	@RequestMapping(value = "/rest2", produces = { BaseSupport.JSON_UTF8 })
	public String rest2(@RequestParam(value = "name", required = false) String name) {
		System.out.println("rest2");
		System.out.println(name);
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("name", name);

		return jsonObject.toString();

	}
	
	
	//接受POST数据，json格式
	@RequestMapping(value = "/rest3", produces = { BaseSupport.JSON_UTF8 },method = RequestMethod.POST)
	public String rest3(@ModelAttribute("user") User user) {
		System.out.println("rest3");
		System.out.println(user.toString());
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("user", user.toString());

		return jsonObject.toString();

	}
	
}
